﻿
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EMSSevice;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics;
using Sunny.UI;
using Newtonsoft.Json.Linq;
using AntdUI;
using BusinessService.Plugin.Services.Dtos;
using BusinessService.Plugin.Services;

namespace WindowsForm.Forms
{
    public partial class MonitorForm : UserControl
    {

        private TransferDtos _backDtos => Program.IOC.GetRequiredService<TransferDtos>();

        private BackService _backService => Program.IOC.GetRequiredService<BackService>();
        public MonitorForm()
        {
            InitializeComponent();
        }

        private void MonitorForm_Load(object sender, EventArgs e)
        {
            switch1.Checked = Settings1.Default.SendAuto;
            inputCycle.Text = Settings1.Default.SendCycle.ToString();
            input1.DataBindings.Add("text", _backDtos, "RealTimeMessage");
            uploadCount.DataBindings.Add("text", _backDtos, "SendCount");
            failCount.DataBindings.Add("text", _backDtos, "ErrorCount");
            //switch1.DataBindings.Add("Checked", _backDtos, "AutoSend");
            //inputCycle.DataBindings.Add("text", _backDtos, "SendCycle");



            Task.Run(() =>
            {
                Job();
            });

        }


        /// <summary>
        /// 定时任务-获取计算机信息
        /// </summary>
        public async void Job()
        {
            while (true)
            {
                // 获取CPU使用率
                // 获取当前进程  
                Process currentProcess = Process.GetCurrentProcess();


                // 创建当前进程的CPU使用率性能计数器  
                using (PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName))
                {
                    // 第一次调用NextValue()可能会返回不准确的值，因为它需要初始化  
                    cpuCounter.NextValue();
                    Thread.Sleep(1000); // 等待一段时间以确保获取到有效的CPU使用率  

                    // 获取当前进程的CPU使用率  
                    float cpuUsage = cpuCounter.NextValue();
                    labelCPU.Text = $"CPU: {cpuUsage:F2}%";   // 注意：在多核CPU上，这个值可能会超过100%，因为它表示的是所有核心上的平均使用率 
                }

                // 输出当前进程的内存使用情况  
                //labelMemory.Text = $"物理内存使用量(工作集):{currentProcess.WorkingSet64 / 1024 / 1024} MB"; // 转换为MB  
                //labelHardDisk1.Text = $"虚拟内存使用量: {currentProcess.VirtualMemorySize64 / 1024 / 1024} MB"; // 转换为MB  
                labelHardDisk2.Text = $"内存: {currentProcess.PrivateMemorySize64 / 1024 / 1024} MB"; // 转换为MB  
                await Task.Delay(1000);
            }
        }

        private async void button1_ClickAsync(object sender, EventArgs e)
        {
            await _backService.PostDataAsync();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            var query = MessageBox.Show("确定要重启应用吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (query == DialogResult.No) return;

            //开启新的实例  
            System.Diagnostics.Process.Start(Application.ExecutablePath);
            //关闭当前实例    
            System.Diagnostics.Process.GetCurrentProcess().Kill();
            Application.Exit();//退出当前项目，如果是子项目，则不会停止主项目
            System.Environment.Exit(0);//停止所有项目


        }

        private void switch1_CheckedChanged(object sender, bool value)
        {
            Settings1.Default.SendAuto = value;
            TransferDtos.AutoSend = Settings1.Default.SendAuto;
            Settings1.Default.Save();
        }

        private void inputCycle_TextChanged(object sender, EventArgs e)
        {
            Settings1.Default.SendCycle = inputCycle.Text.Trim().ToInt();
            TransferDtos.SendCycle = Settings1.Default.SendCycle;
            Settings1.Default.Save();
        }

        private void panel1_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            _backDtos.RealTimeMessage=string.Empty; 
        }
    }
}
